package com.ddj.dao;

import java.util.ArrayList;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ddj.po.Petpo;
import com.ddj.util.HibernateSessionFactory;

public class Petdao {
//    public boolean loginDo(Petpo po){
//	Session session = HibernateSessionFactory.getSession();
//	String hql = "select new Petpo(name,password) from Petpo" +
//					" where name = :name and password= :password";
//	Query query = session.createQuery(hql);
//	query.setProperties(po);
//	ArrayList al = (ArrayList)query.list();
//	if (al.size()>=1){
//	    session.close();
//	    return true;
//	}
//	session.close();
//	 return false;
//    }
    public boolean loginDo(Petpo po,String hql){
	Session session = HibernateSessionFactory.getSession();
	Query query = session.createQuery(hql);
	query.setProperties(po);
	ArrayList al = (ArrayList)query.list();
	if (al.size()>=1){
	    session.close();
	    return true;
	}
	session.close();
	 return false;
    }
    public boolean saveDo(Petpo po) {
	// TODO Auto-generated method stub
	Session session = HibernateSessionFactory.getSession();
	Transaction tc = session.beginTransaction();
	try {
	    session.saveOrUpdate(po);
	    tc.commit();
	    session.close();
	    return true;
	} catch (HibernateException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    tc.rollback();
	    session.close();
	}
	return false;
    }

    public Petpo queryPet(String name,String value) {
	// TODO Auto-generated method stub
	Session session = HibernateSessionFactory.getSession();
	Petpo petpo=new Petpo();
	String hql = "from Petpo where "+name+"=?";
	Query query = session.createQuery(hql);
	query.setString(0, value);
	ArrayList al = (ArrayList)query.list();
	petpo = (Petpo) al.get(0);
	return petpo;
    }

//    public Petpo queryPet(Petpo pet) {
//	// TODO Auto-generated method stub
//	Session session = HibernateSessionFactory.getSession();
//	Petpo petpo=new Petpo();
//	String hql = "from Petpo where name=:name";
//	Query query = session.createQuery(hql);
//	query.setProperties(pet);
//	ArrayList al = (ArrayList)query.list();
//	petpo = (Petpo) al.get(0);
//	return petpo;
//    }

//public void userCriteria(){
//	Session session = HibernateSessionFactory.getSession();
//	Criteria criteria = session.createCriteria(UserPO.class);
////	criteria.add(Restrictions.gt("id", 2));
////	criteria.add(Restrictions.lt("id", 5));
//	criteria.addOrder(Order.asc("username"));
//	criteria.addOrder(Order.desc("id"));
//	
//	List l =  criteria.list();
//	
//	for(int i = 0; i < l.size(); i++){
//		UserPO user = (UserPO)l.get(i);
//		System.out.println(user.getUsername());
//	}
//	session.close();
//}
//
//public void useDeTachedCriteria(DetachedCriteria deCriteria){
//	Session session = HibernateSessionFactory.getSession();
//	Criteria criteria = deCriteria.getExecutableCriteria(session);
//	
//	List l =  criteria.list();
//	
//	for(int i = 0; i < l.size(); i++){
//		UserPO user = (UserPO)l.get(i);
//		System.out.println(user.getUsername());
//	}
//	session.close();
	
}
